package org.donhuan.rms.ui.model;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

public class RmsLabelprovider extends LabelProvider {
	protected final ITreeAndWorkbenchAdapter getAdapter(Object element) {
		ITreeAndWorkbenchAdapter adapter = null;
		if (adapter instanceof IAdaptable)
			adapter = (ITreeAndWorkbenchAdapter) element;
		if (adapter == null)
			adapter = (ITreeAndWorkbenchAdapter) Platform.getAdapterManager()
					.loadAdapter(element,
							ITreeAndWorkbenchAdapter.class.getName());
		return adapter;
	}

	@Override
	public final String getText(Object element) {
		if(element instanceof String)return element.toString();
		ITreeAndWorkbenchAdapter adapter = getAdapter(element);
		if (adapter == null)
			return "";
		return adapter.getLabel(element);
	}

	@Override
	public Image getImage(Object element) {
		if (element instanceof String) {
			String imageKey = ISharedImages.IMG_OBJ_FILE;
			return PlatformUI.getWorkbench().getSharedImages()
					.getImage(imageKey);
		} else
			return getAdapter(element).getImageDescriptor(element)
					.createImage();
	}
}
